var __STORAGE_VERSION = 1.0;

if (!(typeof Storage == 'object' && Storage.version >= __STORAGE_VERSION)) {

	var Storage = {

		listRecords: function () {
			var arr = [];
			for (var i=0; i<window.localStorage.length; i++) {
				var key = window.localStorage.key(i);
				if (!key)
					continue;

				var rgx = /_ID_.+/;
				if (!rgx.test(key))
					continue;

				var value = window.localStorage.getItem(key);
				value = JSON.parse(value);
				arr.push(value);
			}
			return arr;
		},

		getRecord: function (key) {
			key = '_ID_' + key;
			if (!key)
				return null;

			var value = window.localStorage.getItem(key);
			return JSON.parse(value);
		},

		writeRecord: function (record) {
			var key = '_ID_' + record.key;
			window.localStorage.setItem(key, JSON.stringify(record));
		},

		deleteRecord: function (recordID) {
			var key = '_ID_' + recordID;
			if (!window.localStorage[key])
				return;

			window.localStorage.removeItem(key);
		},

		getPassword: function () {
			var key = '__PASSWORD';
			if (!window.localStorage[key])
				return '';

			return window.localStorage.getItem(key);
		},

		setPassword: function (password) {
			var key = '__PASSWORD';
			window.localStorage.setItem(key, password);
		},

		isPasswordSet: function () {
			var key = '__PASSWORD';
			if (!window.localStorage[key])
				return false;

			return true;
		},

		export: function () {
			var records = window.Storage.listRecords();
			var sRecords = JSON.stringify(records);
			var data = Base64.encode(sRecords);
			return data;
		},

		import: function (data) {
			var sRecords = Base64.decode(data);
			var records = JSON.parse(sRecords);
			for (var i=0; i<records.length; i++) {
				var r = records[i];
				window.Storage.writeRecord(r);
			}
		}
	};

}